﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingClientsWebsiteManager.Admin
{
    public partial class UserManager : System.Web.UI.Page
    {
        private void getAllUsers()
        {
            UserGrid.DataSource = GPSTrackingServerDB.Data.DataRepository.UsersProvider.GetAll();
        }

        private void getAllCountries()
        {
            cmbCountries.DataSource = GPSTrackingServerDB.Data.DataRepository.CountriesProvider.GetAll();
            cmbCountries.DataBind();
        }

        private void getAllStates(int CountryId)
        {
            cmbStates.DataSource = GPSTrackingServerDB.Data.DataRepository.StatesProvider.GetByCountryId(CountryId);
            cmbStates.DataBind();
        }

        private void getAllCities(int StateId)
        {
            cmbCities.DataSource = GPSTrackingServerDB.Data.DataRepository.CitiesProvider.GetByStateId(StateId);
            cmbCities.DataBind();
        }

        private void getAllRoles(string Username)
        {
            GPSTrackingServerDB.Data.RolesByUsersQuery rolesByUsersQuery = new GPSTrackingServerDB.Data.RolesByUsersQuery();
            rolesByUsersQuery.AppendEquals(GPSTrackingServerDB.Entities.RolesByUsersColumn.Username, Username);

            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.RolesByUsers> rolesByUsersEntities = GPSTrackingServerDB.Data.DataRepository.RolesByUsersProvider.Find(rolesByUsersQuery);

            lbxRoles.DataSource = GPSTrackingServerDB.Data.DataRepository.RolesProvider.GetAll();
            lbxRoles.DataBind();


            foreach (RadListBoxItem item in lbxRoles.Items)
            {
                foreach (GPSTrackingServerDB.Entities.RolesByUsers entity in rolesByUsersEntities)
                {
                    if (item.Value == entity.RoleId.ToString())
                    {
                        item.Checked = true;
                    }
                }
            }
        }

        private void getGroupsByUser(string Username)
        {
            //Parent
            RadTreeNode Parent = new RadTreeNode();
            Parent.Text = "Grupos";
            GroupsByUserTree.Nodes.Clear();
            GroupsByUserTree.Nodes.Add(Parent);

            //Groups
            int groupParentNum = 0;
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups> groupsEntities = GPSTrackingServerDB.Data.DataRepository.GroupsProvider.GetByUsernameFromUsersByGroups(Username);

            lbxGroups.DataSource = groupsEntities;
            lbxGroups.DataBind();

            foreach (GPSTrackingServerDB.Entities.Groups groupEntity in groupsEntities)
            {
                RadTreeNode groupParent = new RadTreeNode();
                groupParent.Text = groupEntity.GroupName;
                groupParent.Value = groupEntity.GroupId.ToString();

                GroupsByUserTree.Nodes[0].Nodes.Add(groupParent);
                groupParentNum++;

                GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GroupsByImei> groupsByImeiEntities = GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.GetByGroupId(groupEntity.GroupId);
                GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.DeepLoad(groupsByImeiEntities, true, GPSTrackingServerDB.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingServerDB.Entities.Imei));

                foreach (GPSTrackingServerDB.Entities.GroupsByImei groupsByImeiEntity in groupsByImeiEntities)
                {
                    RadTreeNode imei = new RadTreeNode();
                    imei.Text = groupsByImeiEntity.ImeiSource.Name;
                    imei.Value = groupsByImeiEntity.ImeiSource.Imei;
                    GroupsByUserTree.Nodes[0].Nodes[groupParentNum - 1].Nodes.Add(imei);
                }
            }

            GroupsByUserTree.ExpandAllNodes();
        }

        private void getAllFenceByImei(string Imei)
        {
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFenceByImei> geoFenceByImeiEntities = GPSTrackingServerDB.Data.DataRepository.GeoFenceByImeiProvider.GetByImei(Imei);

            GPSTrackingServerDB.Data.DataRepository.GeoFenceByImeiProvider.DeepLoad(geoFenceByImeiEntities, true, GPSTrackingServerDB.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingServerDB.Entities.GeoFence));

            GeoFenceGrid.DataSource = geoFenceByImeiEntities;
            GeoFenceGrid.DataBind();
        }

        private GPSTrackingServerDB.Entities.Users Invoke_User(GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            Hashtable entity = new Hashtable();
            item.ExtractValues(entity);

            GPSTrackingServerDB.Entities.Users userEntity = new GPSTrackingServerDB.Entities.Users();
            userEntity.Username = entity[GPSTrackingServerDB.Entities.UsersColumn.Username.ToString()].ToString();
            userEntity.Password = entity[GPSTrackingServerDB.Entities.UsersColumn.Password.ToString()].ToString();
            userEntity.Identification = Convert.ToDecimal(entity[GPSTrackingServerDB.Entities.UsersColumn.Identification.ToString()]);
            userEntity.Name = entity[GPSTrackingServerDB.Entities.UsersColumn.Name.ToString()].ToString();
            userEntity.FirstName = entity[GPSTrackingServerDB.Entities.UsersColumn.FirstName.ToString()].ToString();
            userEntity.LastName = entity[GPSTrackingServerDB.Entities.UsersColumn.LastName.ToString()].ToString();
            userEntity.CountryId = Convert.ToInt32(entity[GPSTrackingServerDB.Entities.UsersColumn.CountryId.ToString()]);
            userEntity.StateId = Convert.ToInt32(entity[GPSTrackingServerDB.Entities.UsersColumn.StateId.ToString()]);
            userEntity.CityId = Convert.ToInt32(entity[GPSTrackingServerDB.Entities.UsersColumn.CityId.ToString()]);
            if (entity[GPSTrackingServerDB.Entities.UsersColumn.Phone.ToString()] != null) userEntity.Phone = Convert.ToDecimal(entity[GPSTrackingServerDB.Entities.UsersColumn.Phone.ToString()]);
            if (entity[GPSTrackingServerDB.Entities.UsersColumn.Mobile.ToString()] != null) userEntity.Mobile = Convert.ToDecimal(entity[GPSTrackingServerDB.Entities.UsersColumn.Mobile.ToString()]);
            if (entity[GPSTrackingServerDB.Entities.UsersColumn.Email.ToString()] != null) userEntity.Email = entity[GPSTrackingServerDB.Entities.UsersColumn.Email.ToString()].ToString();
            userEntity.IsActive = Convert.ToBoolean(entity[GPSTrackingServerDB.Entities.UsersColumn.IsActive.ToString()]);


            return userEntity;
        }

        private void getAllVehiculeTypes()
        {
            cmbImeiType.DataSource = GPSTrackingServerDB.Data.DataRepository.ImeiTypeProvider.GetAll();
            cmbImeiType.DataBind();
        }

        private void getAllGpsModelsType()
        {
            cmbGpsModelType.DataSource = GPSTrackingServerDB.Data.DataRepository.GpsModelsProvider.GetAll();
            cmbGpsModelType.DataBind();
        }

        private void Clear_Controls()
        {
            txtUsername.Text = string.Empty;
            txtPassword.Text = string.Empty;

            txtIdentification.Text = string.Empty;
            txtName.Text = string.Empty;
            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtPhone.Text = string.Empty;
            txtMobile.Text = string.Empty;
            txtEmail.Text = string.Empty;
            chkIsActive.Checked = false;

            ViewState["UserUsername"] = null;
            ViewState["UserPassword"] = null;
        }

        private void GetGroupsByUsername(string Username)
        {
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.UsersByGroups> usersByGroupsEntities = GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.GetByUsername(ViewState["UserUsername"].ToString());

            GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.DeepLoad(usersByGroupsEntities, true, GPSTrackingServerDB.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingServerDB.Entities.Groups));

            //Parse
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups> groupsEntities = new GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups>();
            foreach (GPSTrackingServerDB.Entities.UsersByGroups entity in usersByGroupsEntities)
            {
                groupsEntities.Add(entity.GroupIdSource);
            }

            cmbGroups.DataSource = groupsEntities;
            cmbGroups.DataTextField = "GroupName";
            cmbGroups.DataValueField = "GroupId";
            cmbGroups.DataBind();
        }

        private void Save_User()
        {
            try
            {
                GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();
                transactionManager.BeginTransaction();

                GPSTrackingServerDB.Entities.Users userEntity = new GPSTrackingServerDB.Entities.Users();

                userEntity.Username = txtUsername.Text;
                userEntity.OriginalUsername = userEntity.Username;

                if (ViewState["UserPassword"] != null)
                    userEntity.Password = ViewState["UserPassword"].ToString();
                else
                    userEntity.Password = txtPassword.Text;

                userEntity.Identification = Convert.ToDecimal(txtIdentification.Text);
                userEntity.Name = txtName.Text;
                userEntity.FirstName = txtFirstName.Text;
                userEntity.LastName = txtLastName.Text;
                userEntity.CountryId = Convert.ToInt32(cmbCountries.SelectedValue);
                userEntity.StateId = Convert.ToInt32(cmbStates.SelectedValue);
                userEntity.CityId = Convert.ToInt32(cmbCities.SelectedValue);
                if (!string.IsNullOrEmpty(txtPhone.Text)) userEntity.Phone = Convert.ToDecimal(txtPhone.Text);
                if (!string.IsNullOrEmpty(txtMobile.Text)) userEntity.Phone = Convert.ToDecimal(txtMobile.Text);
                userEntity.Email = txtEmail.Text;
                userEntity.IsActive = chkIsActive.Checked;

                if (ViewState["CreationDate"] != null)
                    userEntity.CreationDate = Convert.ToDateTime(ViewState["CreationDate"]);
                else
                    userEntity.CreationDate = DateTime.Now;

                if (ViewState["UserUsername"] == null)
                {
                    GPSTrackingServerDB.Data.DataRepository.UsersProvider.Insert(transactionManager, userEntity);

                    ////Default Group
                    //GPSTrackingServerDB.Entities.UsersByGroups usersByGroups = new GPSTrackingServerDB.Entities.UsersByGroups();
                    //usersByGroups.Username = userEntity.Username;
                    //usersByGroups.GroupId = 1; //Sin categoria

                    //GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.Insert(transactionManager, usersByGroups);
                }
                else
                    GPSTrackingServerDB.Data.DataRepository.UsersProvider.Update(transactionManager, userEntity);


                transactionManager.Commit();


                //Refresh
                getAllUsers();
                UserGrid.DataBind();

                //Clear controls
                Clear_Controls();

                RadNotification1.Text = "Guardado";
                RadNotification1.Show();


            }
            catch (Exception ex)
            {
                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_Error('" + ex.Message + "','User error ...');", true);
            }
        }

        private void Delete_User()
        {
            try
            {
                if (ViewState["UserUsername"] != null)
                {
                    string Username = ViewState["UserUsername"].ToString();

                    GPSTrackingServerDB.Data.DataRepository.UsersProvider.Delete(Username);

                    Clear_Controls();

                    //Refresh
                    getAllUsers();
                    UserGrid.DataBind();

                    RadNotification1.Text = "Eliminado";
                    RadNotification1.Show();
                }
            }
            catch (Exception ex)
            {
                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_Error('" + ex.Message + "','User error ...');", true);
            }

        }

        private void Clear_Group()
        {
            ViewState["GroupID"] = null;
            txtGroup.Text = string.Empty;
        }

        private void Save_Group()
        {
            try {
                GPSTrackingServerDB.Entities.Groups GroupEntity = new GPSTrackingServerDB.Entities.Groups();
                GroupEntity.GroupName = txtGroup.Text;

                if (ViewState["GroupID"] == null)
                {

                    GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();
                    transactionManager.BeginTransaction();

                    GPSTrackingServerDB.Data.DataRepository.GroupsProvider.Insert(transactionManager,GroupEntity);

                    //Group by user
                    GPSTrackingServerDB.Entities.UsersByGroups usersByGroupsEntity = new GPSTrackingServerDB.Entities.UsersByGroups();
                    usersByGroupsEntity.Username = ViewState["UserUsername"].ToString();
                    usersByGroupsEntity.GroupId = GroupEntity.GroupId;

                    GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.Insert(transactionManager,usersByGroupsEntity);

                    transactionManager.Commit();

                }
                else
                {
                    GroupEntity.GroupId = Convert.ToInt32(ViewState["GroupID"]);

                    GPSTrackingServerDB.Data.DataRepository.GroupsProvider.Update(GroupEntity);
                }
                //Clear
                Clear_Group();

                //Refresh tree
                getGroupsByUser(ViewState["UserUsername"].ToString());

                RadNotification1.Text = "Guardado";
                RadNotification1.Show();

            }
            catch (Exception ex)
            {
                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_Error('" + ex.Message + "','User error ...');", true);
            }
        }

        private void Delete_Group()
        { 
            try{

                GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();
                transactionManager.BeginTransaction();

                // Groups by imei
                int GroupID = Convert.ToInt32(ViewState["GroupID"]);
                GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GroupsByImei> GroupsByImeiEntities = GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.GetByGroupId(GroupID);
                GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Delete(transactionManager, GroupsByImeiEntities);

                //Groups by user
                GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.Delete(transactionManager, ViewState["UserUsername"].ToString(), GroupID);

                //Group
                GPSTrackingServerDB.Data.DataRepository.GroupsProvider.Delete(transactionManager, GroupID);

                transactionManager.Commit();

                //Clear
                Clear_Group();

                //Refresh tree
                getGroupsByUser(ViewState["UserUsername"].ToString());

                RadNotification1.Text = "Eliminado";
                RadNotification1.Show();

            }
            catch (Exception ex)
            {
                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_Error('" + ex.Message + "','User error ...');", true);
            }
        }

        private void Clear_Imei()
        {
            txtImei.Text = string.Empty;
            txtLicensePlate.Text = string.Empty;
            txtSIMNumber.Text = string.Empty;
            txtDescription.Text = string.Empty;                       
        }


        private void Save_Imei()
        {
            GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();

            transactionManager.BeginTransaction();

            GPSTrackingServerDB.Entities.Imei imeiEntity = new GPSTrackingServerDB.Entities.Imei();
            imeiEntity.OriginalImei = imeiEntity.Imei = txtImei.Text;
            imeiEntity.Name = txtLicensePlate.Text;
            imeiEntity.SimNumber = Convert.ToDecimal(txtSIMNumber.Text);
            imeiEntity.Description = txtDescription.Text;
            imeiEntity.GpsModelId = Convert.ToInt16(cmbGpsModelType.SelectedValue);
            imeiEntity.ImeiTypeId = Convert.ToInt32(cmbImeiType.SelectedValue);

            GPSTrackingServerDB.Data.DataRepository.ImeiProvider.Update(transactionManager, imeiEntity);

            //Delete Group                
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GroupsByImei> groupsByImeiEntites = GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.GetByImei(imeiEntity.Imei);
            GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Delete(groupsByImeiEntites);

            GPSTrackingServerDB.Entities.GroupsByImei groupsByImeiEntity = new GPSTrackingServerDB.Entities.GroupsByImei();
            groupsByImeiEntity.Imei = imeiEntity.Imei;
            groupsByImeiEntity.GroupId = Convert.ToInt32(cmbGroups.SelectedValue);

            GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Insert(transactionManager, groupsByImeiEntity);

            transactionManager.Commit();

            string Username = ViewState["UserUsername"].ToString();
            getGroupsByUser(Username);


            RadNotification1.Text = "Guardado";
            RadNotification1.Show();
        }

        private void Delete_Imei()
        {
            GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Delete(Convert.ToInt32(cmbGroups.SelectedValue), txtImei.Text);

            //Clear
            Clear_Imei();

            //Refresh tree
            getGroupsByUser(ViewState["UserUsername"].ToString());

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                getAllCountries();

                getAllStates(Convert.ToInt32(cmbCountries.SelectedValue));

                getAllCities(Convert.ToInt32(cmbStates.SelectedValue));

                getAllVehiculeTypes();

                getAllGpsModelsType();
            }
        }

        protected void UserGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            getAllUsers();
        }

        protected void UserGrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "UserEdit")
            {
                GPSTrackingServerDB.Entities.Users userEntity = Invoke_User(e);                

                ViewState["UserUsername"] = userEntity.Username;
                ViewState["UserPassword"] = userEntity.Password;
                ViewState["CreationDate"] = userEntity.CreationDate;


                txtUsername.Text = userEntity.Username;
                txtPassword.Text = userEntity.Password;

                txtIdentification.Text = userEntity.Identification.ToString();
                txtName.Text = userEntity.Name;
                txtFirstName.Text = userEntity.FirstName;
                txtLastName.Text = userEntity.LastName;
                cmbCountries.SelectedValue = userEntity.CountryId.ToString();
                cmbStates.SelectedValue = userEntity.StateId.ToString();
                cmbCities.SelectedValue = userEntity.CityId.ToString();
                txtPhone.Text = userEntity.Phone.ToString();
                txtMobile.Text = userEntity.Mobile.ToString();
                txtEmail.Text = userEntity.Email;
                chkIsActive.Checked = userEntity.IsActive;

                //Roles
                getAllRoles(userEntity.Username);


                //Groups
                getGroupsByUser(userEntity.Username);

                //Load Combobox
                GetGroupsByUsername(userEntity.Username);

            }
        }

        protected void cmbCountries_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            getAllStates(Convert.ToInt32(cmbCountries.SelectedValue));
        }

        protected void cmbStates_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            getAllCities(Convert.ToInt32(cmbStates.SelectedValue));
        }

        protected void GroupsByUserTree_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            if (e.Node != null)
            {
                GPSTrackingServerDB.Entities.Imei imeiEntity = GPSTrackingServerDB.Data.DataRepository.ImeiProvider.GetByImei(e.Node.Value);

                txtImei.Text = imeiEntity.Imei;
                txtLicensePlate.Text = imeiEntity.Name;
                txtSIMNumber.Text = imeiEntity.SimNumber.ToString();
                txtDescription.Text = imeiEntity.Description;
                cmbImeiType.SelectedValue = imeiEntity.ImeiTypeId.ToString();
                cmbGpsModelType.SelectedValue = imeiEntity.GpsModelId.ToString();

                //Load GeoFence
                getAllFenceByImei(imeiEntity.Imei);

                GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GroupsByImei> groupsByImeiEntites = GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.GetByImei(imeiEntity.Imei);

                if (groupsByImeiEntites.Count > 0) cmbGroups.SelectedValue = groupsByImeiEntites[0].GroupId.ToString();
            }

        }

        protected void lbxRoles_ItemCheck(object sender, RadListBoxItemEventArgs e)
        {
            GPSTrackingServerDB.Entities.RolesByUsers rolesByUsersEntity = new GPSTrackingServerDB.Entities.RolesByUsers();
            rolesByUsersEntity.RoleId = Convert.ToInt32(e.Item.Value);
            rolesByUsersEntity.Username = ViewState["UserUsername"].ToString();


            if (e.Item.Checked)
            {
                GPSTrackingServerDB.Data.DataRepository.RolesByUsersProvider.Insert(rolesByUsersEntity);
            }
            else
            {
                GPSTrackingServerDB.Data.DataRepository.RolesByUsersProvider.Delete(rolesByUsersEntity);
            }
        }

        protected void RadToolBar1_ButtonClick(object sender, RadToolBarEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Nuevo": Clear_Controls(); break;
                case "Guardar": Save_User(); break;
                case "Eliminar": Delete_User(); break;
            }
        }

        
        protected void btnSaveGroup_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtGroup.Text))
            {

                string Username = ViewState["UserUsername"].ToString();

                GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();
                transactionManager.BeginTransaction();

                GPSTrackingServerDB.Entities.Groups groupsEntity = new GPSTrackingServerDB.Entities.Groups();
                groupsEntity.GroupName = txtGroup.Text;

                GPSTrackingServerDB.Data.DataRepository.GroupsProvider.Insert(transactionManager, groupsEntity);

                GPSTrackingServerDB.Entities.UsersByGroups usersByGroupsEntity = new GPSTrackingServerDB.Entities.UsersByGroups();
                usersByGroupsEntity.GroupId = groupsEntity.GroupId;
                usersByGroupsEntity.Username = Username;

                GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.Insert(transactionManager, usersByGroupsEntity);

                transactionManager.Commit();

                txtGroup.Text = string.Empty;

                getGroupsByUser(Username);

                GetGroupsByUsername(Username);
            }
        }

        protected void RadToolBar2_ButtonClick(object sender, RadToolBarEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Nuevo": Clear_Group(); break;
                case "Guardar": Save_Group(); break;
                case "Eliminar": Delete_Group(); break;
            }
        }

        protected void lbxGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["GroupID"] = lbxGroups.SelectedValue;
            txtGroup.Text = lbxGroups.SelectedItem.Text;
        }

        protected void RadToolBar3_ButtonClick(object sender, RadToolBarEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Nuevo": Clear_Imei(); break;
                case "Guardar": Save_Imei(); break;
                case "Eliminar": Delete_Imei(); break;
            }
        }
    }
}